import {maxShowScrolltop} from 'common/js/config'
import {mapActions, mapGetters} from 'vuex'

export const scrollTop = {
  data() {
    return {
      scrollTop: 0
    }
  },
  computed: {
    showScrollTop() {
      if (this.scrollTop < maxShowScrolltop) {
        return true
      } else {
        return false
      }
    }
  },
  methods: {
    scrolltoTop(time) {
      this.scrollTop = 0
      if (typeof time !== 'number') {
        time = 500
      }
      this._scrollToTop(time)
    },
    _scrollToTop(time) {
      setTimeout(() => {
        this.scrollToTop(time)
      })
    }
  }
}

// 检测是否登录
export const isLogin = {
  data() {
    return {
      isLogin: false
    }
  },
  mounted() {
    this.$nextTick(() => {
      if (this.userInfo.user_token) {
        this.isLogin = true
      } else {
        this.isLogin = false
      }
    })
  },
  computed: {
    ...mapGetters([
      'userInfo'
    ])
  },
  watch: {
    userInfo(val) {
      if (val.user_token) {
        this.isLogin = true
      } else {
        this.isLogin = false
      }
    }
  }
}

// 检测是否登录 弹出提示窗 与isLogin一起引用
export const checkLogin = {
  methods: {
    _checkLogin() {
      if (!this.isLogin) {
        return this.checkConfirmLogin(true).then(() => {
          return false
        })
      } else {
        return new Promise((resolve) => {
          resolve(true)
        })
      }
    },
    ...mapActions([
      'checkConfirmLogin'
    ])
  }
}
// 弹出提示
export const Alert = {
  methods: {
    ...mapActions([
      'showAlert'
    ])
  }
}
// 加载提示
export const Loading = {
  methods: {
    ...mapActions([
      'showLoading',
      'closeLoading'
    ])
  }
}
